using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace caseDrawer
{
	public partial class senarioForm : Form
	{
		Senario senario;
		string testCaseFolder = "c:\\";
		List<Events> eventList = new List<Events>();

		public Senario Senario{
			set{this.senario = value;}
		}

		public senarioForm( Senario senario)
		{
			InitializeComponent();
			eventList = getEvents();

			this.senario = senario;

		}


		public void renderTabPage(){
			senarioTabs.SelectedTab = createNewTab();
			loadSenarioInformation();

			senarioTabs.SelectedTab.Text = senario.Id;
		}

		TabPage createNewTab(){
			TabPage newTab = new TabPage();

			DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();

			this.panel1 		= new Panel();
			this.brickGridView 	= new DataGridView();
			this.cid 			= new DataGridViewTextBoxColumn();
			this.cdescription 	= new DataGridViewTextBoxColumn();
			this.caccessEvents 		= new DataGridViewComboBoxColumn();
			this.cisrequired 	= new DataGridViewCheckBoxColumn();
			this.chasInTimeValidation 	= new DataGridViewCheckBoxColumn();
			this.panel2 		= new Panel();
			this.label1 		= new Label();
			this.txtSenario 	= new TextBox();

			//this.senarioTabs.SuspendLayout();
			newTab.SuspendLayout();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.brickGridView)).BeginInit();
			this.panel2.SuspendLayout();
			this.SuspendLayout();

			//
			// senarioTabs
			//
			this.senarioTabs.Controls.Add(newTab);

			this.senarioTabs.Appearance = TabAppearance.Buttons;
			this.senarioTabs.Dock = DockStyle.Fill;
			this.senarioTabs.Location = new System.Drawing.Point(0, 0);
			this.senarioTabs.Name = "senarioTabs";
			this.senarioTabs.SelectedIndex = 0;
			this.senarioTabs.Size = new System.Drawing.Size(618, 526);
			this.senarioTabs.TabIndex = 1;
			this.senarioTabs.SelectedIndexChanged += new System.EventHandler(this.SenarioTabsSelectedIndexChanged);

			//
			// newTab
			//
			newTab.BackColor = System.Drawing.SystemColors.Window;
			newTab.BorderStyle = BorderStyle.Fixed3D;
			newTab.Controls.Add(this.panel1);
			newTab.Controls.Add(this.panel2);
			newTab.Location = new System.Drawing.Point(4, 25);
			newTab.Name = "newTab";
			newTab.Padding = new Padding(3);
			newTab.Size = new System.Drawing.Size(610, 497);
			newTab.TabIndex = 0;
			newTab.Text = "New Senario";
			newTab.UseVisualStyleBackColor = true;
			//
			// panel1
			//
			this.panel1.BackColor = System.Drawing.SystemColors.ScrollBar;
			this.panel1.Controls.Add(this.brickGridView);
			this.panel1.Dock = DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(3, 118);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(600, 372);
			this.panel1.TabIndex = 5;
			//
			// brickGridView
			//
			this.brickGridView.BackgroundColor = System.Drawing.SystemColors.Window;
			this.brickGridView.BorderStyle = BorderStyle.None;
			this.brickGridView.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
			dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleLeft;
			dataGridViewCellStyle1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			dataGridViewCellStyle1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
			dataGridViewCellStyle1.ForeColor = System.Drawing.SystemColors.WindowText;
			dataGridViewCellStyle1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
			dataGridViewCellStyle1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
			dataGridViewCellStyle1.WrapMode = DataGridViewTriState.True;
			this.brickGridView.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;
			this.brickGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.brickGridView.Columns.AddRange(new DataGridViewColumn[] {
									this.cid,
									this.caccessEvents,
									this.cdescription,
									this.cisrequired,
									this.chasInTimeValidation
			                                    });
			this.brickGridView.Dock = DockStyle.Fill;
			this.brickGridView.Location = new System.Drawing.Point(0, 0);
			this.brickGridView.Name = "brickGridView";
			this.brickGridView.RowHeadersVisible = false;
			dataGridViewCellStyle2.SelectionBackColor = System.Drawing.Color.LightGray;
			this.brickGridView.RowsDefaultCellStyle = dataGridViewCellStyle2;
			this.brickGridView.RowTemplate.Height = 23;
			this.brickGridView.Size = new System.Drawing.Size(600, 372);
			this.brickGridView.TabIndex = 3;
			this.brickGridView.KeyDown += new KeyEventHandler(this.BrickGridViewKeyDown);
			this.brickGridView.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler( this.brickGridView_EditingControlShowing );
			this.brickGridView.CellMouseClick += new DataGridViewCellMouseEventHandler(this.brickGridView_CellEnter );		
			//
			// cid
			//
			this.cid.DataPropertyName = "id";
			this.cid.HeaderText = "ID";
			this.cid.Name = "cid";
			this.cid.Resizable = DataGridViewTriState.False;
			this.cid.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.cid.Visible = false;
			this.cid.Width = 5;
			//
			// caccessEvents
			//
			this.caccessEvents.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.caccessEvents.HeaderText = "Event";
			this.caccessEvents.Name = "caccessEvents";
			this.caccessEvents.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.caccessEvents.DataSource = eventList;
			this.caccessEvents.DataPropertyName = "accessEvent";
			this.caccessEvents.DisplayMember = "Name";
			this.caccessEvents.ValueMember = "ID";
			//
			// cdescription
			//
			this.cdescription.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.cdescription.DataPropertyName = "name";
			this.cdescription.HeaderText = "Description";
			this.cdescription.Name = "cdescription";
			this.cdescription.SortMode = DataGridViewColumnSortMode.NotSortable;

			//
			// cisrequired
			//
			this.cisrequired.DataPropertyName = "isrequired";
			this.cisrequired.FalseValue = "0";
			this.cisrequired.HeaderText = "Required";
			this.cisrequired.IndeterminateValue = "0";
			this.cisrequired.Name = "cisrequired";
			this.cisrequired.Resizable = DataGridViewTriState.False;
			this.cisrequired.TrueValue = "1";

			//
			// hasInTimeValidation
			//
			this.chasInTimeValidation.DataPropertyName = "hasInTimeValidation";
			this.chasInTimeValidation.FalseValue = "0";
			this.chasInTimeValidation.HeaderText = "Valid";
			this.chasInTimeValidation.IndeterminateValue = "0";
			this.chasInTimeValidation.Name = "chasInTimeValidation";
			this.chasInTimeValidation.Resizable = DataGridViewTriState.False;
			this.chasInTimeValidation.TrueValue = "1";
			//
			// panel2
			//
			this.panel2.Controls.Add(this.label1);
			this.panel2.Controls.Add(this.txtSenario);
			this.panel2.Dock = DockStyle.Top;
			this.panel2.Location = new System.Drawing.Point(3, 3);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(600, 115);
			this.panel2.TabIndex = 4;
			//
			// label1
			//
			this.label1.BackColor = System.Drawing.SystemColors.Window;
			this.label1.Dock = DockStyle.Top;
			this.label1.Font = new System.Drawing.Font("Calibri", 26F);
			this.label1.Location = new System.Drawing.Point(0, 0);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(600, 37);
			this.label1.TabIndex = 3;
			this.label1.Text = "Senario";
			//
			// txtSenario
			//
			this.txtSenario.BorderStyle = BorderStyle.None;
			this.txtSenario.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F);
			this.txtSenario.Location = new System.Drawing.Point(48, 68);
			this.txtSenario.Multiline = true;
			this.txtSenario.Name = "txtSenario";
			this.txtSenario.Size = new System.Drawing.Size(561, 43);
			this.txtSenario.TabIndex = 2;
			this.txtSenario.Text = "";

			txtSenario.Focus();
			return newTab;
		}

		void loadSenarioInformation(){

			if(this.senario == null){
				senario 			= new Senario();
				senario.Id 			= "0000";
				txtSenario.Text   	= this.senario.Name;
			}
			else{
				txtSenario.Text   			= this.senario.Name;
				brickGridView.DataSource 	= Database.Instance.loadBricks(this.senario);
			}

		}

		void btSaveClick(object sender, EventArgs e)
		{
			string senarioName = senarioTabs.SelectedTab.Controls.Find("txtSenario",true)[0].Text;
			DataGridView dgv   = (DataGridView)(senarioTabs.SelectedTab.Controls.Find("brickGridView",true)[0]);

			this.senario.Name 		= senarioName;
			this.senario.Id = Database.Instance.saveSenario(this.senario);

			foreach(DataGridViewRow row in dgv.Rows){
				if(row.IsNewRow)
					break;

				Brick b = new Brick();
				if(row.Cells[0].Value.ToString()!=null)
					b.Id				= row.Cells[0].Value.ToString();

				b.ContainerId 			= this.senario.Id;
				b.Name					= row.Cells[1].Value.ToString();
				b.IsRequired 			= row.Cells[2].Value==DBNull.Value? false : Convert.ToBoolean(row.Cells[2].Value);
				b.HasInTimeValidataion 	= row.Cells[3].Value==DBNull.Value? "0" : row.Cells[3].Value.ToString();

				row.Cells[0].Value = Database.Instance.saveBrick(b);
			}

			senarioTabs.SelectedTab.Text = senario.Id;

		}

		List<Events> getEvents(){
			List<Events> tmpEvents = new List<Events>
			{
				new Events{ID = "1",Name = "Mouse Hover"},
				new Events{ID = "2",Name = "Mouse Down"},
				new Events{ID = "3",Name = "Mouse Scroll"},
				new Events{ID = "4",Name = "Mouse Leave"},
				new Events{ID = "5",Name = "Key Press"},
				new Events{ID = "6",Name = "Click"},
				new Events{ID = "7",Name = "Double Click"},
				new Events{ID = "8",Name = "Right Click"},
				new Events{ID = "9",Name = "Right Double Click"},
			};

			return tmpEvents;

		}

		void BrickGridViewKeyDown(object sender, KeyEventArgs e)
		{
			if(e.KeyValue != 39)
				return;

			int colindex = ((DataGridView)(sender)).SelectedCells[0].ColumnIndex;

			if(colindex != 4)
				return;

			int curRowIndex = ((DataGridView)(sender)).SelectedCells[0].RowIndex;
			string cur_brickId = brickGridView.Rows[curRowIndex].Cells["cid"].Value.ToString();

			showResultForm(cur_brickId);
		}

		void showResultForm(string cur_brickId){
			ResultForm resultform = new ResultForm(cur_brickId);

			resultform.Show();
		}

		void BtNewClick(object sender, EventArgs e)
		{
			senario = new Senario();

			renderTabPage();
		}

		void BtGenerateClick(object sender, EventArgs e)
		{
			DataGridView dgv = (DataGridView)(senarioTabs.SelectedTab.Controls.Find("brickGridView",true)[0]);
			
			AbstractGenerator generator;
			
			generator = GeneratorFactory.Instance.getGenerator("TC_REQUIRED");
			generator.Senario   		= this.senario;
			generator.Rows				= dgv.Rows;
			generator.TestCaseFolder 	= testCaseFolder;
			generator.generateTestCase();
			
			generator = GeneratorFactory.Instance.getGenerator("TC_FLOW");
			generator.Senario   		= this.senario;
			generator.Rows				= dgv.Rows;
			generator.TestCaseFolder 	= testCaseFolder;
			generator.generateTestCase();
		}

		void SenarioTabsSelectedIndexChanged(object sender, EventArgs e)
		{

		}

		void showSenarioForm(){
			SenariosForm senariofor = new SenariosForm();
			senariofor.Show();
		}

		void BtOpenClick(object sender, EventArgs e)
		{
			showSenarioForm();
		}

		void brickGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			if (e.Control is DataGridViewComboBoxEditingControl)
			{
				(e.Control as DataGridViewComboBoxEditingControl).SelectedIndexChanged += new EventHandler(caccessEvents_SelectedIndexChanged);
			}
		}

		void brickGridView_CellEnter(object sender, EventArgs e){
			brickGridView.BeginEdit(false);
		}

		void caccessEvents_SelectedIndexChanged(object sender, EventArgs e){
			//DataGridViewComboBoxEditingControl combox = (DataGridViewComboBoxEditingControl)(sender);
			//combox.SelectedValue = "2";

		}
		
	}
}
